/*
 * File Name: 		NativeOcr.java
 * 
 * Copyright(c) 2011 Yunmai Co.,Ltd.
 * 
 * 		 All rights reserved.
 * 					
 */

package scan.bcr.ch;

import android.util.Log;


/**
 * @purpose: Native class for BCR engine
 * 
 * @author: chongxishen
 * 
 * @date: Jan. 2009
 */
public class NativeOcr {
	
	private static final String TAG = "NativeOcr";
	
	private static final String LIB = "ocrengine";
	static {
		try {
			System.loadLibrary(LIB);
		} catch (Exception e) {
			Log.e("", "ocrengine", e);
		}
	}
	
	private static int mProgress = 0;
	private static boolean mCancel = false;

	// ////////////////////////////////////////////////////
	// Constructor/Destructor

	public NativeOcr() {
		
	}

	@Override
	public void finalize() {

	}
	
	public static void reset() {
		mProgress = 0;
		mCancel = false;
	}
	
	public static int getProgress() {
		return mProgress;
	}
	
	public static int Progress(int progress, int relative) {

		if (relative != 0) {
			mProgress += progress;
		} else {
			mProgress = progress;
		}

//		OcrEngine.processing(mProgress);

		if (mCancel) {
			reset();
			return 0; // cancel
		}

		return 1;
	}

	/**
	 * NOTE: Use long type instead of Pointer value of the real C pointer. Type
	 * int is also OK, but Use long to be 64-bit safe.
	 * 
	 * Here just list all APIs of OCR Engine, MAYBE will write a class to wrap
	 * this class for better encapsulation and easy usage.
	 */

	public native int startBCR(long[] ppEngine, byte[] pDataPath,
			byte[] pConfigFile, int nLanguage);

	public native int closeBCR(long[] ppEngine);

	public native int doImageBCR(long pEngine, long pImage, long[] ppField);

	public native int freeImage(long pEngine, long[] ppImage);

	public native int imageChecking(long pEngine, long pImage, int level);

	public native void freeBField(long pEngine, long pfield, int once);

	public native int getLastErr(long pEngine, byte[] msg, int msgsize);

	// EXPORT int HC_SetProgressFunc(BEngine *pEngine, int (*func)(int progress,
	// int relative));
	// public native int setProgressFunc(long pEngine, long func);
	
	public native int setoption(long pEngine, byte[] key, byte[] pContent);
	
	public native void setProgressFunc(long pEngine, boolean install); // fixxed

	// by
	// chongxishen
	// on
	// 2009-1-13

	// EXPORT BImage *HC_LoadImageMem(BEngine *pEngine, char *pBuffer, int
	// width, int height)
	// public native long loadImageMem(long pEngine, byte[] pBuffer, int width,
	// int height);
	public native long loadImageMem(long pEngine, long pBuffer, int width,
			int height, int component); // fixxed by chongxishen on

	// These APIs I add for avoiding to access the member variables via pointer
	// Because Java do not allow to use pointer like C
	public native int getFieldId(long pField);

	public native int getFieldText(long pField, byte[] /* out */text,
			int arraysize);
	
	/**
	 * 获取字段区域坐标
	 * @param pField
	 * @param rect		输出坐标
	 * @return
	 */
	public native int getFieldRect(long pField, int[] rect);

	public native long getNextField(long pField);

	// add by chongxishen on 2009-1-13
	public native int getVersionString(byte[] buf, int size);

	public native int codeConvert(long pEngine, byte[] text, int key);

	// search PinYin. add by chongxishen on 2009-2-2
	public native int hasPinYin(long pField);

	public native int getPinYin(long pField, byte[] /* out */text, int arraysize);

	public native long getYData(byte[] pRgb565, int width, int height);

	/*
	 * public native int doImageBlk(long pEngine, long pImage, long ppField);
	 * 
	 * public native int getFieldText(long pEngine, long pImage, long pField,
	 * byte[] text, int size); public native int setFontType(long ppEngine, int
	 * nLanguage); public native int getFontType(long pEngine); public native
	 * int setChineseCode(long pEngine, int codec); public native int
	 * setLetterCase(long pEngine, int mode);
	 * 
	 * public native int setAutoFlip(long pEngine, int flip); public native int
	 * setManualProcess(long pEngine, int manual); public native int
	 * setMultiStep(long pEngine, int mode); public native int
	 * setOcrReadMode(long pEngine, int mode);
	 * 
	 * public native int allocImage(long pEngine, long ppImage, int width, int
	 * height, int setting);
	 * 
	 * //EXPORT BImage *HC_LoadImage_BMP(BEngine *pEngine, char *filename);
	 * //public native int loadImage_BMP(long pEngine, byte[] filename);
	 * 
	 * //EXPORT BImage *HC_LoadImageBMP(BEngine *pEngine, char *pBitmap)
	 * //public native int loadImageBMP(long pEngine, byte[] pBitmap);
	 * 
	 * public native int printFieldInfo(long pEngine, long pField, byte[] pText,
	 * int size); public native int printFieldDetail(long pEngine, long pField);
	 * 
	 * public native int imageConvert(long pEngine, long pImage, int nLanguage);
	 * public native int getLanSupport(long pEngine, byte[] libpath, long lan);
	 */

	// EXPORT BField *HC_GetBField(BField *pfield, BFieldID fid);
	// public native long getBField(long pfield, int fid); //
	// ///////////////////////////////////////////////////////////////////////
	// / OCR for documentation
	/*
	 * public native int startOCR(long ppEngine, char *pDataPath, char
	 * *pConfigFile, int nLanguage, int nReader); public native int
	 * closeOCR(long pEngine); public native int doLineOCR(long pEngine, long
	 * pImage, char *text, int size); public native int doImageOCR(long pEngine,
	 * long pImage, long ppField);
	 * 
	 * public native int enableMultiLine(long pEngine, int mode); public native
	 * int enableRotate(long pEngine, int mode); public native int
	 * setImgType(long pEngine, int m_SmallImage); public native int
	 * setImageFile(long pEngine, char *pImageFile); public native int
	 * removeUnderLine(long pEngine, int m_RMoveLine);
	 */
	// / avoid complier error
	// public static void main(String args[])
	// {
	// NativeOcr ocr = new NativeOcr();
	// ocr.startBCR()
	// }
	// EXPORT int HC_ImageConvert(BEngine *pEngine, BImage *pImage, int
	// nLanguage);
	public native int ImageConvert(long pEngine, long pImage, int nLanguage);

	// EXPORT int HC_CodeConvert(BEngine *pEngine, char *text, int key);
	public native int CodeConvert(long pEngine, byte[] text, int key);

	// EXPORT int HC_DoLineOCR(BEngine *pEngine, BImage *pImage, char *text, int
	// size);
	public native int DoLineOCR(long pEngine, long pImage, byte[] text, int size);
	
	/**
	 * 图像增强
	 * @param pImage
	 * @return
	 */
	public native int HistEqualizationForRGB(long pImage);
	 
	/**
	 * 切边和矫正
	 * @param pEngine		
	 * @param pImage		
	 * @param pCropRectOut	矫正后的图形坐标
	 * @param pAngleOut		矫正值（除以100）
	 * @return
	 */
	public native int GetCropAndDeskewInfo(long pEngine, long pImage,int[] pCropRectOut,long pAngleOut);

}